/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Manish Mehta 
 												
Class							:				GameStateHelpers
 
Function						: 				A helper class for extracting information 
												from the game state 
****************************************************************************/
package behavioradaptation.reasoner;

import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import base.CBLILRModule;
import base.WargusGameState;
import base.WargusPlayer;
import base.WargusStateImporter;
import base.WargusUnit;
import cbr.Coordinate;

public class GameStateHelpers {
	/*
	 * Get a Unit Id of certain type from the game State
	 */
	public static List<Integer> GetUnitIDofType(WargusGameState a_gameState,int a_playerID,String a_unitType)
	{
		List<Integer> return_value = new LinkedList<Integer>();	
		WargusPlayer p = a_gameState.getPlayer(a_playerID);
		
		for(WargusUnit u:p.getUnits()) {
			if (WargusStateImporter.unitTypeToString(u.getType()).equals(a_unitType)) {
				return_value.add(new Integer(u.getUnitID()));
			} 
		}
		return return_value;
	}
	
	/*
	 * Get a Idle Unit of certain type from the game State
	 */
	public static List<Integer> GetIdleUnitIDofType(Node a_gameState,int a_playerID,String a_unitType)
	{
		Node players = CBLILRModule.XMLfeatureValue(a_gameState, "players");
		NodeList player_l=players.getChildNodes();
		int i,pll;
		List<Integer> return_value = new LinkedList<Integer>();
		
		pll=player_l.getLength();
		for(i=0;i<pll;i++) {
			Node player=player_l.item(i);			
			NamedNodeMap att_l=player.getAttributes();
			Node id_n=att_l.getNamedItem("id");
			if (id_n!=null) {
				if (Integer.parseInt(id_n.getNodeValue()) == a_playerID) {
					Node units=CBLILRModule.XMLfeatureValue(player, "units");
					NodeList unit_l=units.getChildNodes();
					int j,ull;
					
					ull=unit_l.getLength();
					for(j=0;j<ull;j++) {
						Node unit=unit_l.item(j);
						Node type = CBLILRModule.XMLfeatureValue(unit, "type");
						Node tTmp = type.getFirstChild();
						Node status = CBLILRModule.XMLfeatureValue(unit, "status");
						Node tTmp2 = status.getFirstChild();
						if (tTmp.getNodeValue().equals(a_unitType) &&
							tTmp2.getNodeValue().equals("idle")) {
							NamedNodeMap att_l2 = unit.getAttributes();
							Node id_n2 = att_l2.getNamedItem("id");
							return_value.add(new Integer(Integer.parseInt(id_n2.getNodeValue())));
						}
					}
				}
			}
		}
		return return_value;
	}
	
	/*
	 * Get a Gold IDs from the game State
	 */
	public static List<Integer> GoldMinesID(Node a_gameState)
	{
		Node map = CBLILRModule.XMLfeatureValue(a_gameState, "map");
		Node gold = CBLILRModule.XMLfeatureValue(map, "gold");
		NodeList gold_mines = gold.getChildNodes();
		List<Integer> return_value = new LinkedList<Integer>();
		int i;
		
		for(i=0;i<gold_mines.getLength();i++) {
			Node goldmine = gold_mines.item(i);
			NamedNodeMap att_l = goldmine.getAttributes();
			Node id_n = att_l.getNamedItem("id");
			return_value.add(new Integer(Integer.parseInt(id_n.getNodeValue())));
		}
		
		return return_value;
	}
	/*
	 * Get a Gold Mine's XY coordinates
	 */
	public static List<Coordinate> GoldMinesXY(WargusGameState a_gameState)
	{
		List<Coordinate> PairList = new LinkedList<Coordinate>();
		
		for(WargusUnit gm:a_gameState.getMap().get_goldMines()) {			
			Coordinate P = new Coordinate();
			P.x = gm.getLocX();
			P.y = gm.getLocY();
			PairList.add(P);
		}	
		return PairList;
	}	

}